﻿using GodSharp;
using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZK.Common;
using ZK.Common.Log4;

namespace ZK.HardWareHelp
{
    public class WagonBalanceOld
    {
        private readonly GodSerialPort _serialPort;
        private bool _isOpened;//打开状态
        private List<double> _data = new List<double>();
        public decimal weighNow = 0;
        public WagonBalanceOld(HaredWareCommonM config)
        {
            _serialPort =
                new GodSerialPort(config.PortName, Convert.ToInt32(config.BaudRate), Parity.None, Convert.ToInt32(config.DataBits), StopBits.One)
                {
                    EndCharOfHex = "03",
                    DataFormat = SerialPortDataFormat.Hex,
                    RtsEnable = true,
                    DtrEnable = true
                };
            _serialPort.UseDataReceived((serialPort, bytes) =>
            {
                try
                {
                    if (_data.Count > 99)
                        _data.RemoveAt(0);
                    var weight = GetWeight(bytes);

                    Common.DelegateHelper.WeightCallbackEvent?.Invoke(weight);
                    _data.Add(weight);
                }
                catch (Exception ex)
                {
                    LogManage.WriteErr("获取地磅数据异常：" + ex.Message.ToString());
                }
            });
            Open();
        }
        /// <summary>
        /// 获取地磅数据
        /// </summary>
        /// <param name="bytes"></param>
        /// <returns></returns>
        private double GetWeight(byte[] bytes)
        {
            if (bytes.Length == 12)
            {
                var number = 0;
                number += (bytes[3] - 48) * 10000;
                number += (bytes[4] - 48) * 1000;
                number += (bytes[5] - 48) * 100;
                number += (bytes[6] - 48) * 10;
                number += bytes[7] - 48;
                return (double)number / 1000;
                //return number;

            }
            else
            {
                Common.Log4.LogManage.WriteErr("获取地磅数据长度有误");
                return -1;
            }
        }
        /// <summary>
        /// 打开地磅
        /// </summary>
        public void Open()
        {
            _isOpened = _serialPort.Open();
            ReopenIfClosed();
        }
        /// <summary>
        /// 关闭地磅
        /// </summary>
        public void Close()
        {
            _isOpened = !_serialPort.Close();
        }
        /// <summary>
        /// 重新打开
        /// </summary>
        private void ReopenIfClosed()
        {
            if (_isOpened)
            {
                //await Task.Delay(1000);
                //await Task.Run(() =>
                //{
                //    if (!_serialPort.IsOpen)
                //    {
                //        _serialPort.Close();
                //        _serialPort.Open();
                //    }
                //    ReopenIfClosed();
                //});
            }
            else
            {
                _serialPort.Close();
            }
        }
    }
}
